#ifndef _MODULE_PAGE
#define _MODULE_PAGE
#include "prefs.module.js"
#include "boss.module.js"
#include "menu.module.js"
#include "console.module.js"
#include "mwcaptcha.module.js"
#include "log.lib.js"
#include "ajax.lib.js"
#include "utility.lib.js"
#include "usolib.lib.js"

var Page = {
	domain: "http://apps.facebook.com",
	captcha_data: {
		data: '',
		url: ''
	},
	special_pause: false,
	captcha_src: '',
	pausing: false,
	pause_cbs: [],
	init: function() {
		Menu.init();
		Preferences.update();
		if (Settings.insider)
			stamina_wait * 0.9;
		Log.memory = Settings.log_memory;
		Mwcaptcha.username = Settings.mwc_username;
		Mwcaptcha.password = Settings.mwc_password;
	},
	toggle: function(script) {
		if (eval(script.ucwords() + '.running')) {
			eval(script.ucwords() + '.log("Stopped running")');
			eval(script.ucwords() + '.running=false');
			$('#status_' + script).html('Click to begin');
			$('#mwt_' + script).attr('class', '');
		} else {
			var c_script = eval(script.ucwords());
			c_script.init();
			$('#mwt_' + script).attr('class', 'running');
		}
		var working = false;
		for (var i = Modules.length - 1; i >= 0; i--) {
			if (eval(Modules[i] + '.running')) {
				working = true;
			}
		}
		if (working) $('#mwt_working').css({
			display: 'block'
		}).show();
		else $('#mwt_working').hide();
	},
	bcast: function(script) {
		return eval(script.ucwords() + '.running');
	},
	wait: function() {
		return Math.floor(Math.random() * ( Settings.max_refresh - Settings.min_refresh + 1 ) + Settings.min_refresh);
	},
	pause_cb: function(cb) {
		for (var i = Page.pause_cbs.length - 1; i >= 0; i--) {
			if (Page.pause_cbs[i].toSource() == cb.toSource())
				return;
		}
		Page.pause_cbs.push(cb);
	},
	resume: function() {
		Page.pausing = false;
		Page.special_pause = false;
		for (var i = Page.pause_cbs.length - 1; i >= 0; i--) {
			var cb = Page.pause_cbs[i];
			cb();
		}
		Page.pause_cbs = [];
		return;
	},
	halt: function() {
		Array.forEach(Modules, function(module) {
			eval(module + '.running=false');
			return;
		});
		return;
	},
	captcha: function(html) {
		if ( html.indexOf('Are You Human?') !== -1 ) {
			if (Page.pausing) return;
			Page.pausing = true;
			var image = html.match( new RegExp('src="(http://.*?/img/captcha.*?)"') )[1].replace( new RegExp('&amp;', 'g'), '&' );
			Page.captcha_data.url = html.match( new RegExp('method="post" action="(.*?)"') )[1];
			if (m = html.match( new RegExp('type="hidden" (?:name="action"|value="(.*?)") (?:name="action"|value="(.*?)")') )) {
				if (m[1]) {
					action = m[1];
				} else {
					action = m[2];
				}
			}
			Page.captcha_src = image;
			if (Settings.mwcaptcha === true && Settings.mwc_use === true) {
				$('#captcha_image').hide();
				$('#captcha_answer')[0].disabled = true;
				$('#captcha_answer')[0].value = "Waiting to be answered";
				$('#captcha_submit').hide();
				Mwcaptcha.add(Page.captcha_src);
				$('#mwt_captcha_box').show();
			} else {
				$('#captcha_answer')[0].disabled = false;
				$('#captcha_answer')[0].value = '';
				$('#captcha_submit').show();
				$('#captcha_image').attr('src', image).show();
				$('#mwt_captcha_box').show();
				$('#captcha_answer')[0].focus();
			}
			Page.captcha_data.image = encodeURIComponent(image);
			Page.captcha_data.data = 'cap_action=bust_cap&action=' + action + '&cap_answer=';
			if (Settings.captcha_sound) {
				Page.captcha('play');
			}
			return true;
		}
		else if ( html.indexOf('Enter the number in the box') !== -1 ) {
			if (Page.pausing) return;
			Page.pausing = true;
			Page.captcha_data.url = html.match( new RegExp('method="post" action="(.*?)"') )[1];
			var image = html.match( new RegExp('src="(http://.*?/img/captcha_image.*?)"') )[1].replace( new RegExp('&amp;', 'g'), '&');
			Page.captcha_src = image;
			if (Settings.mwcaptcha === true && Settings.mwc_use === true) {
				$('#captcha_image').hide();
				$('#captcha_answer')[0].disabled = true;
				$('#captcha_answer')[0].value = "Waiting to be answered";
				$('#captcha_submit').hide();
				Mwcaptcha.add(Page.captcha_src);
				$('#mwt_captcha_box').show();
			} else {
				$('#captcha_answer')[0].disabled = false;
				$('#captcha_answer')[0].value = '';
				$('#captcha_submit').show();
				$('#captcha_image').attr('src', image).show();
				$('#mwt_captcha_box').show();
				$('#captcha_answer')[0].focus();
			}
			Page.captcha_data.image = escape(image);
			Page.captcha_data.data = 'action=allow_access&cap_value=';
			if (Settings.captcha_sound) {
				Page.captcha('play');
			}
			return true;
		}
		else if ( html.indexOf('This page is being heavily rate limited') !== -1 ) {
			Page.special_pause = true;
			Page.pausing = true;
			$('#a_title').html('Page being heavily rate limited!');
			$('#a_content').html('This Mob Wars page is being heavily rate limited. Wait for the timer to finish or click \'Retry\' to continue');
			$('#mwt_a').show();
			if (Console.a_timer) {
				Console.a_timer.stop();
			}
			Console.a_timer = new Utility.a_timer({
				time: 180,
				message: 'Retry [[time]]',
				fn: function() {
					Page.resume();
					Console.a_close();
					return;
				}
			});
			Console.a_timer.start();
			return true;
		} else if (html == 'play') {
			if (Page.pausing) {
				Utility.play_sound('http://www.mwturbo.com/sound.wav');
				setTimeout(Page.captcha, 180000, 'play');
			}
		}
		return false;
	},
	captcha_submit: function(answer) {
		$('#captcha_image')[0].src = '';
		answer = answer || $('#captcha_answer')[0].value;
		$('#mwt_captcha_box').slide_out();
		Page.captcha_data.answer = answer;
		Ajax(Page.captcha_data.url + '?' + Page.captcha_data.data + answer, function(xhr) {
			Page.resume();
			Boss.update(xhr.responseText);
			Page.captcha(xhr.responseText);
			if (Settings.mwcaptcha === true && Settings.mwc_use === true) {
				Page.send_catpcha();
			}
			return;
		},
		'POST');
		return;
	},
	send_catpcha: function() {
		if (Page.pausing != true) {
			Mwcaptcha.result(true);
		} else {
			Mwcaptcha.result(false);
		}
		return;
	},
	show_mwcaptcha: function() {
		Page.mwcaptcha_showing = true;
		$('#mwcaptcha_image')[0].src = Mwcaptcha.image;
		$('#mwcaptcha_answer')[0].value = "";
		$('#mwc_captcha_box').show();
		$('#mwcaptcha_answer')[0].focus();
		if (Settings.captcha_sound) {
			Utility.play_sound('http://www.mwturbo.com/sound.wav');
		}
		return;
	},
	mwcaptcha_submit: function() {
		$('#mwcaptcha_image')[0].src = "";
		Page.mwcaptcha_showing = false;
		Mwcaptcha.solution = $('#mwcaptcha_answer')[0].value;
		Mwcaptcha.answer();
		$('#mwc_captcha_box').slide_out();
	}
};
#endif
